const app = getApp()
const util = require('../../utils/util.js')

Page({

  data: {
    post: null,
    options: null,
    postDate: null,
  },

  onLoad: function (options) {
    var that = this
    this.setData({
      options: options,
    })
    wx.showLoading({
      title: '加载数据中...',
      success: this.getPostById(that, options.postId)
    })
  },

  // 下拉刷新
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading() // 在标题栏中显示加载
    this.onLoad(this.data.options)
  },

  // 分享
  onShareAppMessage: function (res) {
    var post = this.data.post
    return {
      title: post.title,
      imageUrl: post.cover
    }
  },

  // 获取单篇图文
  getPostById: function (that, id) {
    wx.request({
      url: app.globalData.url + 'index/post/id/' + id,
      method: 'GET',
      success: function (res) {
        var post = res.data.data
        that.setData({
          post: post,
          postDate: util.formatTime(new Date(post.updatetime * 1000))
        })
        wx.hideLoading()
        wx.hideNavigationBarLoading()
        wx.stopPullDownRefresh()
      },
      fail: function (e) {
        console.log(e)
      }
    })
  },

})